from django.shortcuts import render
from rest_framework.views import APIView
from meiduo.libs.captcha.captcha import captcha
from django_redis import get_redis_connection
from django.http.response import HttpResponse

from . import constants

# Create your views here.


class ImageView(APIView):
    """图片验证码视图"""

    def get(self, request, image_code_id):
        # 生成图片验证码
        test, image = captcha.generate_captcha()
        redis_conn = get_redis_connection("verifications")
        # 此处无需捕获异常
        redis_conn.setex("img_%s" % image_code_id, constants.IMAGE_CODE_EXPIRE_TIME, test)

        return HttpResponse(image,content_type="image/jpg")